#! /usr/local/bin/python
#-*- coding: utf-8 -*-

"""Interface graphique pour le tableau des Highscores.
"""

"""
Minesweeper 3K. Original implementation of the classic game of mines
for Python 3.
Copyright (C) 2010  Cedric Bonhomme

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

Minesweeper 3K  Copyright (C) 2010  Cedric Bonhomme
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
"""

__author__ = "Jerome Hussenet, Cedric Bonhomme"
__date__ = "$Date: 2010/02/27 $"
__copyright__ = "Copyright (c) 2010 Jerome Hussenet, Copyright (c) 2010 Cedric Bonhomme"
__license__ = "GPL 3"

import operator

from tkinter import *

textParams = {
    "highlightthickness" : 0 ,
    "font" : ("courier", 12)
}

class Score(Tk):
    """Interface graphique pour le tableau des Highscores.
    """
    def __init__(self, parent):
        """Initialisation de l'interface.
        """
        self.frame = Toplevel()
        self.frame.title(_('Meilleurs scores'))
        #self.minsize(width = 350, height = 100)
        self.frame.resizable(height = False, width = False)

        #ListBox contenant les niveaux de Jeux.
        listboxScroll = Scrollbar(self.frame, orient = VERTICAL)
        listboxScroll.grid(column = 2, row = 0, sticky = W)
        self.listbox = Listbox(self.frame, yscrollcommand=listboxScroll.set, height = 1, width = 25)
        self.listbox.grid(column = 1, row = 0, sticky = W + E)
        listboxScroll['command'] = self.listbox.yview
        for item in ["Debutant", "Intermediaire", "Expert"]:
            item1 = (28 - int(round(len(item)/2))) * " " + item
            self.listbox.insert(END, item1)
        self.listbox.bind("<ButtonRelease-1>", self.affiche)

        self.listeLabel = []
        self.label = Label(self.frame, text = _('\nChoisissez un niveau.\n') , \
                 width = 25, padx = 50, **textParams)
        self.label.grid(row=1, columnspan = 2)

        ## fixe le focus sur la fenêtre (obligatoire pour Windows)
        self.frame.focus_set()
        ## Force les évènements à aller sur cette fenêtre
        self.frame.grab_set()
        ## Pour que la fenêtre reste au-dessus de sa fenêtre parente
        self.frame.transient(parent)
        ## Affiche la fenêtre et attend sa fermeture
        self.frame.wait_window(self.frame)

    def affiche(self, event, mode = 'debutant'):
        """Affiche les scores.

        Les scores sont affichés sous forme de tableau
        dans un Label.
        """
        import score
        try: # on récupère le niveau sélectionné
            mode = self.listbox.get(int(self.listbox.curselection()[0])) \
                    .replace(' ', '').lower()
        except IndexError:
            pass

        x = score.Score() # objet servant à manipuler le fichier XML des scores
        listeScores =  [(elem.prenom, elem.temps) for elem in x.getPersonnes(mode)]
        listeScores.sort(key=operator.itemgetter(1)) # trie sur les scores

        if listeScores: # s'il y a des scores
            resultat = _('Joueurs') + ' ' * 10 + _('Temps\n')
            for personne in listeScores:
                resultat = resultat + '\n'  + \
                    ' ' * (25 - 2 - len(personne[0])) + personne[0] + \
                    ' ' * 8 + str(personne[1]) + ' ' * (25 - 6 - len(str(personne[1])))
            self.label.configure(text=resultat)
        else:
            self.label.configure(text=_('\nAucun scores pour ce niveau.\n'))

if __name__ == '__main__':
    score = Score()